<?php defined('SYSPATH') or die('No direct script access.');

class Model_ProductParser_Shop extends Model_ProductParser_Abstract
{ 
    protected static $_instance = null;
    
    public static function get_category_popular_products(Model_Shop $shop, $url)
    {
        $ids = array();
        
        if ($shop && method_exists(__CLASS__, '_parse_'.$shop->id)) {
            $ids = call_user_func(array(__CLASS__, '_parse_'.$shop->id), $url);
        }
        return $ids;
    }
    
    protected static function _parse_1001($url)
    {
        // 60 штук
        $ids = $tmp_ids = $tmp_ids2 = array();
        $source = Model_ProductParser_Abstract::_get_source_code($url);
        
        $source = Model_ProductParser_Abstract::_cut_str('<div class="products-catalog__list">', '<div class="products-catalog__foot"', $source);
        while ($id = Model_ProductParser_Abstract::_cut_str('href="/p/', '"', $source)) {
            $tmp_ids[] = $id;    
        }
        $n = count($tmp_ids);
        $maxsort = 300000;
        for ($i = 0; $i < $n; $i++) {
            $id   = $tmp_ids[$i];
            //$sort = (int)(($n - $i-1)/($n/10)) + 1;
            $sort = $maxsort - $i;
            $tmp_ids2[$id] = array(
                'id'   => $id,
                'sort' => $sort,
            ); 
        }
        foreach ($tmp_ids2 as $item) {
            $product = DB::select('id', 'original_id', 'url')
                        ->from('products')
                        ->where('url', 'LIKE', DB::expr("'https://ad.admitad.com/goto/3f2779c2d4ea57d2938c4e8640d77b/?ulp=http%3A%2F%2Fwww.lamoda.ru%2Fp%2F".$item['id']."%'"))     
                        ->limit(1)
                        ->execute()
                        ->as_array();
            if (isset($product[0])) {
                $product = $product[0];
                $ids[] = array(
                    'id'   => $product['original_id'],
                    'sort' => $item['sort'],
                );    
            }
            sleep(1);
            //usleep(500000);
        }
        return $ids;
    }
    
    protected static function _parse_4102($url)
    {
        // 100 штук
        $ids = $tmp_ids = array();
        $source = Model_ProductParser_Abstract::_get_source_code($url);
        
        $source = Model_ProductParser_Abstract::_cut_str('<div class="catalog_main_table"', '<div id="catalog-overlay">', $source);
        while ($id = Model_ProductParser_Abstract::_cut_str("<div id='c", "'", $source)) {
            $tmp_ids[] = $id;    
        }
        $n = count($tmp_ids);
        $maxsort = 300000; 
        for ($i = 0; $i < $n; $i++) {
            $id   = $tmp_ids[$i];
            //$sort = (int)(($n - $i-1)/($n/10)) + 1;
            $sort = $maxsort - $i; 
            $ids[] = array(
                'id'   => $id,
                'sort' => $sort,
            );  
        }
        return $ids;
    }
    
    protected static function _parse_4965($url)
    {
        // 204 штуки
        $ids = $tmp_ids = $tmp_ids2 = array();
        $source = Model_ProductParser_Abstract::_get_source_code($url);
        
        $source = Model_ProductParser_Abstract::_cut_str('<ul id="items">', '</ul>', $source); 
        while ($id = Model_ProductParser_Abstract::_cut_str('class="desc" href="', '&amp;cid=', $source)) {
            $id        = str_replace(array(':', '/', '?', '='), array('%3A', '%2F', '%3F', '%3D'), $id);
            $tmp_ids[] = $id;    
        }
        $n = count($tmp_ids);
        $maxsort = 300000;
        for ($i = 0; $i < $n; $i++) {
            $id   = $tmp_ids[$i];
            $sort = $maxsort - $i;
            $tmp_ids2[$id] = array(
                'id'   => $id,
                'sort' => $sort,
            ); 
        }
        foreach ($tmp_ids2 as $item) {
            $product = DB::select('id', 'original_id', 'url')
                        ->from('products')
                        ->where('url', '=', DB::expr("'https://ad.admitad.com/goto/df5e25dad0ea57d2938c1a45f50765/?ulp=".$item['id']."'"))     
                        ->where('shop_id', '=', 4965)
                        ->limit(1)
                        ->execute()
                        ->as_array();
            if (isset($product[0])) {
                $product = $product[0];
                $ids[] = array(
                    'id'   => $product['original_id'],
                    'sort' => $item['sort'],
                );    
            }
            sleep(1);
            //usleep(500000);
        }
        return $ids;
    }
}
